﻿using LpbPrj.Client.Models;
using System;
using System.Globalization;
using System.Windows.Data;

namespace LpbPrj.Client.Tools.Converter
{
    public class AScanConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            object output = null;
            if (value == null) return output;

            {
                if (value is IolMasterALineResult item)
                {
                    if (parameter == null) return output;
                    var param = System.Convert.ToString(parameter);
                    if (string.IsNullOrEmpty(param)) return output;

                    if (param.Equals("AL")) output = item.AL.ToShowString(2, "-");
                    if (param.Equals("CCT")) output = item.CCT.ToShowString(0, "-");
                    if (param.Equals("AD")) output = item.AD.ToShowString(2, "-");
                    if (param.Equals("LT")) output = item.LT.ToShowString(2, "-");
                    if (param.Equals("VT")) output = item.VT.ToShowString(2, "-");
                    if (param.Equals("RT")) output = item.RT.ToShowString(0, "-");

                }
            }
            {
                if (value is IolMasterKeraResult item)
                {
                    if (parameter == null) return output;
                    var param = System.Convert.ToString(parameter);
                    if (string.IsNullOrEmpty(param)) return output;

                    if (param.Equals("FlatK")) output = item.FlatK.ToShowString(2, "-");
                    if (param.Equals("FlatR")) output = item.FlatR.ToShowString(2, "-");
                    if (param.Equals("FlatA")) output = item.FlatA.ToShowString(0, "-");

                    if (param.Equals("SteepK")) output = item.SteepK.ToShowString(2, "-");
                    if (param.Equals("SteepR")) output = item.SteepR.ToShowString(2, "-");
                    if (param.Equals("SteepA")) output = item.SteepA.ToShowString(0, "-");

                    if (param.Equals("AST")) output = $"{item.AST.ToShowString(2, "-")}D @ {item.ASTA.ToShowString(0, "-")}°";
                    if (param.Equals("Pupil")) output = item.Pupil.ToShowString(2, "-");

                }
            }
            {
                if (value is IolMasterRefResult item)
                {
                    if (parameter == null) return output;
                    var param = System.Convert.ToString(parameter);
                    if (string.IsNullOrEmpty(param)) return output;

                    ////////////////////////////////////////电脑验光////////////////////////////////////
                    double? sphOutput = item.SPH.ToDouble();
                    double? cylOutput = item.CYL.ToDouble();
                    double? axsOutput = item.AXS.ToDouble();

                    if (sphOutput.Value > 0)
                    {
                        sphOutput = sphOutput.Value + 0.25 * 0.5;
                    }
                    if (sphOutput.Value <= 0)
                    {
                        sphOutput = sphOutput.Value - 0.25 * 0.5;
                    }

                    if (cylOutput.Value > 0)
                    {
                        cylOutput = cylOutput.Value + 0.25 * 0.5;
                    }
                    if (cylOutput.Value <= 0)
                    {
                        cylOutput = cylOutput.Value - 0.25 * 0.5;
                    }

                    ////////////////////////////////////////通用展示////////////////////////////////////
                    if (param.Equals("SPH") && sphOutput.HasValue)
                    {
                        double? data = ((int)(sphOutput.Value * 1000)) / 250 * 250 / 1000.0;
                        output = data.ToFormatPowerString(2, "-", awayFromZero: false);
                    }
                    if (param.Equals("CYL") && cylOutput.HasValue)
                    {
                        double? data = ((int)(cylOutput.Value * 1000)) / 250 * 250 / 1000.0;
                        output = data.ToFormatPowerString(2, "-", awayFromZero: false);
                    }
                    if (param.Equals("AXS") && axsOutput.HasValue)
                    {
                        int? data = axsOutput.Value.ToInt();
                        output = data.ToShowString("-");
                    }
                    if (param.Equals("SE"))
                    {
                        double? data = sphOutput + cylOutput * 0.5;
                        data = (int)(data * 1000) / 250 * 250 / 1000.0;
                        output = data.ToFormatPowerString(2, "-", awayFromZero: false);
                    }
                    if (param.Equals("Vertex")) output = item.Vertex.ToShowString(2, "-");
                    if (param.Equals("Score")) output = item.Score;

                }
            }
            {
                if (value is IolMasterWTWResult item)
                {
                    if (parameter == null) return output;
                    var param = System.Convert.ToString(parameter);
                    if (string.IsNullOrEmpty(param)) return output;

                    ////////////////////////////////////////通用展示////////////////////////////////////
                    output = item.WTW.ToShowString(2, substitutes: "-");
                }
            }
            
            return output;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }


    }

    /// <summary>
    /// 验光数据转换
    /// </summary>
    public static class RefCorrect
    {
        /// <summary>
        /// ver的12mm状态转换成0mm状态
        /// </summary>
        /// <param name="sph"></param>
        /// <param name="cyl"></param>
        /// <param name="ver"></param>
        /// <returns></returns>
        public static (double? sph, double? cyl, double? ver) VertexCorrect(double? sph, double? cyl, double? ver)
        {
            double? sphere = null;
            if (sph.HasValue && ver.HasValue)
            {
                sphere = VertexCorrect(sph.Value, ver.Value);
            }

            double? cylinder = null;
            if (sph.HasValue && ver.HasValue && cyl.HasValue)
            {
                cylinder = VertexCorrect(cyl.Value + sph.Value, ver.Value) - sphere.Value;
            }
            return (sphere.ToDouble(), cylinder.ToDouble(), 0.0);
        }

        private static double? VertexCorrect(double power, double vertex)
        {
            return power / (1.0 - vertex * power / 1000.0);
        }

        /// <summary>
        /// 把距离Ver的屈光度转换成0mm对应的值
        /// </summary>
        /// <param name="sph"></param>
        /// <param name="cyl"></param>
        /// <param name="ver"></param>
        /// <returns></returns>
        public static (double? sph, double? cyl) VertexCorrectVerToZero(double? sph, double? cyl, double? ver)
        {
            double? sphere = null;
            if (sph.HasValue && ver.HasValue)
            {
                sphere = sph / (1 - ver / 1000.0 * sph);
            }

            double? cylinder = null;
            if (sph.HasValue && ver.HasValue && cyl.HasValue && sphere.HasValue)
            {
                cylinder = (sph + cyl) / (1 - ver / 1000.0 * (sph + cyl)) - sphere;
            }

            return (sphere.ToDouble(), cylinder.ToDouble());
        }

        /// <summary>
        /// 把距离0mm的屈光度转换成Ver对应的值
        /// </summary>
        /// <param name="sph"></param>
        /// <param name="cyl"></param>
        /// <param name="ver"></param>
        public static (double? sph, double? cyl) VertexCorrectZeroToVer(double? sph, double? cyl, double? ver)
        {
            double? sphere = null;
            if (sph.HasValue && ver.HasValue)
            {
                sphere = sph / (1 + ver / 1000.0 * sph);
            }

            double? cylinder = null;
            if (sph.HasValue && ver.HasValue && cyl.HasValue && sphere.HasValue)
            {
                cylinder = (sph + cyl) / (1 + ver / 1000.0 * (sph + cyl)) - sphere;
            }

            return (sphere.ToDouble(), cylinder.ToDouble());
        }

        /// <summary>
        /// 负表示转正表示
        /// </summary>
        /// <param name="sph"></param>
        /// <param name="cyl"></param>
        /// <param name="axs"></param>
        /// <returns></returns>
        public static (double? sph, double? cyl, double? axs) ConvertToPlusCylForm(double? sph, double? cyl, double? axs)
        {
            double? sphOutput = sph;
            double? cylOutput = cyl;
            double? axsOutput = axs;

            if (cyl.HasValue && !double.IsNaN(cyl.Value))
            {
                double cylinder = cyl.Value;
                if (cylinder < 0.0 && !double.IsNaN(cylinder))
                {
                    double num = Math.Abs(cylinder);
                    cylOutput = num;

                    if (sph.HasValue && !double.IsNaN(sph.Value))
                    {
                        sphOutput -= num;
                    }
                    if (axs.HasValue && !double.IsNaN(axs.Value))
                    {
                        double axis = axs.Value;
                        if (axis < 90.0)
                        {
                            axsOutput = axis + 90.0;
                        }
                        else
                        {
                            axsOutput = axis - 90.0;
                        }
                    }
                }
            }

            return (sphOutput.ToDouble(), cylOutput.ToDouble(), axsOutput.ToDouble());
        }

        /// <summary>
        /// 正表示转负表示
        /// </summary>
        /// <param name="sph"></param>
        /// <param name="cyl"></param>
        /// <param name="axs"></param>
        /// <returns></returns>
        public static (double? sph, double? cyl, double? axs) ConvertToMinusCylForm(double? sph, double? cyl, double? axs)
        {
            double? sphOutput = sph;
            double? cylOutput = cyl;
            double? axsOutput = axs;

            if (cyl.HasValue)
            {
                double cylinder = cyl.Value;
                if (cylinder > 0.0)
                {
                    if (sph.HasValue)
                    {
                        sphOutput += cylinder;
                    }
                    cylOutput = -cylinder;

                    if (axs.HasValue)
                    {
                        double axis = axs.Value;
                        if (axis < 90.0)
                        {
                            axsOutput = axis + 90.0;
                        }
                        else
                        {
                            axsOutput = axis - 90.0;
                        }
                    }
                }
            }

            return (sphOutput, cylOutput, axsOutput);
        }

        /// <summary>
        /// 计算每个角度的屈光度
        /// </summary>
        /// <param name="angle"></param>
        /// <param name="axis"></param>
        /// <param name="cyl"></param>
        /// <param name="sph"></param>
        /// <returns></returns>
        public static double PowerAtAngle(double angle, double axis, double cyl, double sph)
        {
            return (Math.Cos(((angle - axis) * 2.0 - 180.0) * 0.01745329251994) + 1.0) * (cyl * 0.5) + sph;
        }



    }

}
